package com.loubii.sunshineSchool.ui.card;

import android.content.DialogInterface;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.loubii.sunshineSchool.FinalVirable;
import com.loubii.sunshineSchool.R;
import com.loubii.sunshineSchool.adapter.SSMatesAdapter;
import com.loubii.sunshineSchool.constants.CardRes;
import com.loubii.sunshineSchool.httpRequest.HttpBin;
import com.loubii.sunshineSchool.pojo.ActivityDetail;
import com.loubii.sunshineSchool.pojo.ActivityItem;
import com.loubii.sunshineSchool.ui.avtivity.BaseActivity;
import com.loubii.sunshineSchool.util.ImageUtil;
import com.loubii.sunshineSchool.view.BaseToolbar;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

public class SSDetailActivity extends BaseActivity {


    @BindView(R.id.toolbar)
    BaseToolbar mToolbar;
    @BindView(R.id.tv_transfer)
    TextView mTvTransfer;
    @BindView(R.id.btn_post)
    Button post;
    @BindView(R.id.ultimate_recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_bandcard)
    ImageView mIvBandcard;
    @BindView(R.id.tv_bandcard_name)
    TextView mTvBandcardName;
    @BindView(R.id.tv_bandcard_type)
    TextView mTvBandcardType;
    @BindView(R.id.tv_bandcard_id)
    TextView mTvBandcardId;
    @BindView(R.id.tv_done)
    TextView mTvDone;
    @BindView(R.id.activity_content)
    TextView text;
    @BindView(R.id.numOfPost)
    TextView activePerson;
    @BindView(R.id.item_card)
    RelativeLayout cardLayout;
    ActivityDetail detail;
    ActivityItem item;

    @Override
    protected void initData() {
        item = (ActivityItem) getIntent().getSerializableExtra("detail");
        HttpBin.searchOneActivity(this, item.getBid(), new HttpBin.ResponseListener<ActivityDetail>() {
            @Override
            public void onResponse(Call<ActivityDetail> call, Response<ActivityDetail> response) {
                detail = response.body();

                text.setText(detail.getText());
                if (detail.getNum() == -1) {
                    activePerson.setVisibility(View.GONE);
                    initRecyclerView();
                    recyclerView.setVisibility(View.VISIBLE);
                    post.setVisibility(View.GONE);
                } else {
                    activePerson.setText("当前有" + detail.getNum() + "人报名，你也试试吧~");
                    activePerson.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                    post.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void onFailure(Call<ActivityDetail> call, Throwable t) {

            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_card_detail;
    }

    @Override
    protected void initView() {

        String type = FinalVirable.types.get(item.getType());
        mTvBandcardName.setText(item.getUserName());
        mTvBandcardType.setText(type);
        mTvBandcardId.setText(item.getTitle());
        mTvDone.setVisibility(View.VISIBLE);
        mTvTransfer.setVisibility(View.VISIBLE);
        if (item.getState() != 1)
            mTvDone.setVisibility(View.GONE);
        if (item.getState() != 0)
            mTvTransfer.setVisibility(View.GONE);
        cardLayout.setBackgroundResource(CardRes.getCardBg(type));
        ImageUtil.downloadAvatar(item.getImage(), mIvBandcard);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final EditText editText = new EditText(SSDetailActivity.this);
                editText.setHint("100字以内哦~");
                editText.setHeight(300);
                AlertDialog.Builder inputDialog =
                        new AlertDialog.Builder(SSDetailActivity.this);
                inputDialog.setTitle("请输入理由~").setView(editText);
                inputDialog.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                HttpBin.sign(SSDetailActivity.this, item.getBid(), editText.getText().toString(), new HttpBin.ResponseListener<Boolean>() {
                                    @Override
                                    public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                                        if (response.body()) {
                                            Toast.makeText(SSDetailActivity.this,
                                                    "报名成功啦~",
                                                    Toast.LENGTH_LONG).show();
                                        } else {
                                            Toast.makeText(SSDetailActivity.this,
                                                    "你已经报名啦，不用重复报名哦~",
                                                    Toast.LENGTH_LONG).show();
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<Boolean> call, Throwable t) {

                                    }
                                });
                            }
                        }).show();
            }
        };
        post.setOnClickListener(listener);
        mTvTransfer.setOnClickListener(listener);
    }

    private void initRecyclerView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        SSMatesAdapter mAdapter = new SSMatesAdapter(this, detail.getItems(), item.getBid());
        recyclerView.setAdapter(mAdapter);
    }

    protected void initToolBar() {
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            //去除默认Title显示
            actionBar.setDisplayShowTitleEnabled(false);
            //返回按钮
            actionBar.setDisplayHomeAsUpEnabled(true);
            //actionBar.setHomeAsUpIndicator(R.mipmap.ic_toolbar_left);
        }
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mToolbar.setCenterTitle("活动详情");
    }


}
